package com.shujia.java.day20.demo6;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
* @Description: TCPSocket 客户端向服务端发送 两个数字 服务器给客户端返回一个结果
* @Author: 王火火
* @Date: 2019/8/23 15:14
 *
*/
public class TCPSocket {
    public static void main(String[] args) throws Exception{
        //创建Socket
        Socket socket = new Socket("127.0.0.1",9000);
        //创建流
        OutputStream os = socket.getOutputStream();
        byte[] bytes;
        //手动输入数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个数字:");
        bytes=scanner.next().getBytes();
        System.out.println("第一个值为:"+new String(bytes,0,bytes.length));
        //传输两个数据
        os.write(bytes,0,bytes.length);
        //用10把原来的1替换掉 形成一次传递一个数字
        System.out.println("请输入第二个数字:");
        bytes=scanner.next().getBytes();
        os.write(bytes,0,bytes.length);
        System.out.println("第二个值为:"+new String(bytes,0,bytes.length));
        //接收服务器端传递的值
        //通过Socket获取getInputStream
        InputStream is = socket.getInputStream();
        int len=is.read(bytes,0,bytes.length);
        System.out.println("结果为:"+new String(bytes,0,len));

    }
}
